package api

import (
	"360-cinema/video-server/basic/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
	"path/filepath"
)

func Upload(c *gin.Context) {

	file, err := c.FormFile("image")
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 0,
			"msg":  "查找失败",
		})
		return
	}
	if file.Size > 1024*1024*10 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 0,
			"msg":  "图片过大",
		})
		return
	}
	ext := filepath.Ext(file.Filename)
	if ext != ".mp4" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 0,
			"msg":  "文件格式不正确",
		})
		return
	}
	upload, err := pkg.Upload(file)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 0,
			"msg":  "上传失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "上传成功",
		"data": upload,
	})
}
